package com.javafactroy.kunlong.fastdfs;

import com.javafactroy.kunlong.fastdfs.dto.FileInfoDTO;
import com.javafactroy.kunlong.fastdfs.iface.FileApiService;
import com.javafactroy.kunlong.fastdfs.service.FastDFSService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class FileFdfsService implements FileApiService {

    @Value("${fastdfs.fileserver:http://www.kunlong.top:8888}")
    private String fileServer;

    public FileInfoDTO upload(String name, String ext, byte[] bytes, Map<String, String> props) {
        FileInfoDTO r = new FileInfoDTO();
        try {
            String path = FastDFSService.upload(name, ext, bytes, null);
            r.setExt(ext);
            r.setName(name);
            r.setPath(fileServer + "/" + path);
            r.setSize((long) bytes.length);
            return r;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public byte[] download(String groupName, String remoteFileName) {
        try {
            byte[] content = FastDFSService.download(groupName, remoteFileName);
            return content;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

}